package com.qumitech.visualnovel.service.risk.imp;

import java.util.*;
import java.util.HashMap;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.shiro.SecurityUtils;
import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.service.exception.ExceptionService;
import com.qumitech.visualnovel.controller.vo.risk.UserAlarmAddDTO;
import com.qumitech.visualnovel.service.risk.RiskUserAlarmService;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.datasource.console.RiskAlarmConfMapper;
import com.qumitech.visualnovel.datasource.console.RiskAlarmDataMapper;
import com.qumitech.visualnovel.datasource.console.RiskChatmsgDataDOMapper;
import com.qumitech.visualnovel.model.dao.console.RiskAlarmConf;
import com.qumitech.visualnovel.model.dao.console.UserInfo;
import com.qumitech.visualnovel.model.dao.console.RiskChatmsgDataDO;

@Service
public class RiskUserAlarmServiceImpl implements RiskUserAlarmService {
    @Autowired
    private RiskAlarmConfMapper riskAlarmConfMapper;
    @Autowired
    private RiskChatmsgDataDOMapper riskChatmsgDataDOMapper;

    @Override
    public BaseRsp queryRiskUserAlarmConf(String alarmType, String alarmValue, String startTime, String endTime, String offset, String limit) {
        int count = riskAlarmConfMapper.countRiskUserAlarmConf(alarmType, alarmValue, startTime, endTime);
        List<RiskAlarmConf> listRiskAlarmConf = riskAlarmConfMapper.queryRiskUserAlarmConf(alarmType, alarmValue, startTime, endTime, Integer.parseInt(offset), Integer.parseInt(limit));
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("count", count);
        jsonObj.put("items", listRiskAlarmConf);
        return new BaseRsp(ResultEnum.SUCCESS, jsonObj);
    }

    @Override
    public BaseRsp addRiskUserAlarmConf(UserAlarmAddDTO addDTO) {
        Subject subject = SecurityUtils.getSubject();
        UserInfo activeUser = (UserInfo) subject.getPrincipal();

        RiskAlarmConf riskAlarmConf = new RiskAlarmConf();
        riskAlarmConf.setType(addDTO.getAlarmType());
        riskAlarmConf.setOperator(activeUser.getUsername());
        riskAlarmConf.setRemarks(addDTO.getAlarmRemarks());

        String[] alarmValues = addDTO.getAlarmValue().split(",");
        for (String alarmValue : alarmValues) {
            riskAlarmConf.setValue(alarmValue);
            riskAlarmConfMapper.addRiskUserAlarmConf(riskAlarmConf);
        }

        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @Override
    public BaseRsp delRiskUserAlarmConf(String autoid) {
        riskAlarmConfMapper.deleteByPrimaryKey(Long.valueOf(autoid));
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @Override
    public BaseRsp queryRiskUserChats(String content, String sender, String receiver, String status, String startTime, String endTime, String offset, String limit) {
        int count = riskChatmsgDataDOMapper.countRiskUserChats(content, sender, receiver, status, startTime, endTime);
        List<RiskChatmsgDataDO> riskUserChatsData = riskChatmsgDataDOMapper.queryRiskUserChats(content, sender, receiver, status, startTime, endTime, Integer.parseInt(offset), Integer.parseInt(limit));
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("count", count);
        jsonObj.put("items", riskUserChatsData);
        return new BaseRsp(ResultEnum.SUCCESS, jsonObj);
    }

    @Override
    public void forbiddenUser(Long sender) {
        riskChatmsgDataDOMapper.delRiskUserChatsBySender(sender);
    }

    @Override
    public void forbiddenUsers(Long[] senders) {
        for (int idx=0; idx<senders.length; ++idx) {
            riskChatmsgDataDOMapper.delRiskUserChatsBySender(senders[idx]);
        }
    }

    @Override
    public BaseRsp queryRiskUserChatsAlarm() {
        RiskAlarmConf alarmConf = riskAlarmConfMapper.queryRiskUserChatsAlarm();
        List<RiskChatmsgDataDO> riskChatmsgs = riskChatmsgDataDOMapper.queryRiskUserChatsAlarm(Integer.parseInt(alarmConf.getValue()));

        if (riskChatmsgs.size() > 0) {
            Long maxAutoid = riskChatmsgs.get(riskChatmsgs.size()-1).getId();
            RiskAlarmConf newAlarmConf = new RiskAlarmConf();
            newAlarmConf.setAutoid(alarmConf.getAutoid());
            newAlarmConf.setValue(String.valueOf(maxAutoid));
            riskAlarmConfMapper.updateByPrimaryKeySelective(newAlarmConf);
        }

        JSONObject jsonObj = new JSONObject();
        jsonObj.put("count", riskChatmsgs.size());
        jsonObj.put("items", riskChatmsgs);
        return new BaseRsp(ResultEnum.SUCCESS, jsonObj);
    }
}
